/**
	提示节点管理
**/

var TipNodeMediator = cc.Class({
    extends: cc.Component,
    name: 'TipNodeMediator',
    properties: {
        selectNumLabel:{
            default:null,
            type: cc.Label
        },
        scoreLabel:{
            default:null,
            type: cc.Label
        },
        levelLabel:{
            default:null,
            type: cc.Label
        },
        bestScoreLabel:{
            default:null,
            type: cc.Label
        },
        needScoreLabel:{
            default:null,
            type: cc.Label
        }
    },
    onLoad: function () {
        //基础分数配置
        this.oneStarScore = 10;
        this.extraScore = 5;
        this.atLeastExtraNum = 2;
        this.totalScore = 0;
        //最佳分
        this.bestScore = 0;
        this.level = 0;
        //没关所需分数加成
        this.baseAddScore = 2000;
        this.initBestScore();
        this.setLevelTip();
    },
    setSelectNum:function(num){
        //设置提示信息
        var score = this.caculateScore(num);
        this.selectNumLabel.string = num + " 连消 " + score + " 分"
    },
    caculateScore:function(num){
        //计算分数
        var oneStarScore = this.oneStarScore + ((num - this.atLeastExtraNum) * this.extraScore);
        return oneStarScore * num;
    },
    addScore:function(num){
        //清空提示信息
        this.selectNumLabel.string = "";
        var score = this.caculateScore(num);
        this.totalScore += score;
        this.scoreLabel.string = "Score: "+this.totalScore;
        this.changeBestScore();
    },
    setLevelTip:function(){
        this.level++;
        this.levelLabel.string = "Level: "+this.level;
        this.setPassScore();
    },
    initBestScore:function(){
        this.bestScore = cc.sys.localStorage.getItem("BestScore");
        if(this.bestScore) this.bestScoreLabel.node.active = true;
        else this.bestScoreLabel.node.active = false;
        this.bestScoreLabel.string = "BestScore: " + this.bestScore;
    },
    changeBestScore:function(){
        //如果当前有显示最佳分数，则判断当前分数有没有超过，超过则替换显示
        if(this.bestScoreLabel.node.active && this.bestScore < this.totalScore) {
            this.bestScoreLabel.string = "BestScore: " + this.totalScore;
        }
        if(!this.bestScore || this.bestScore < this.totalScore) cc.sys.localStorage.setItem("BestScore",this.totalScore);
    },
    getPassScore:function(){
        return this.level * this.baseAddScore - 1000;
    },
    setPassScore:function(){
        var needScore = this.getPassScore();
        this.needScoreLabel.string = "NeedScore: " + needScore;
    }
});

module.exports = TipNodeMediator;